﻿using System;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace badminton
{
    public partial class DateTime_EditField : System.Web.DynamicData.FieldTemplateUserControl
    {
        private static DataTypeAttribute DefaultDateAttribute = new DataTypeAttribute(DataType.DateTime);
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.ToolTip = Column.Description;


            if(!this.IsPostBack)
            {
                for (int i = 0; i < 24; i++)
                {
                    DropDownListHour.Items.Add(new ListItem(i.ToString()));
                }

                for (int i = 0; i < 60; i++)
                {
                    DropDownListMinute.Items.Add(new ListItem(i.ToString()));
                }
            }         
        }


        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            if (this.FieldValue != null )
            {
                DateTime dt = (DateTime) FieldValue;
                this.DropDownListHour.SelectedIndex = dt.Hour;
                this.DropDownListMinute.SelectedIndex = dt.Minute;
            }
        }

        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            try
            {
                if (TextBox1.Text.Trim().Length>0)
                {
                    DateTime dt = DateTime.Parse(TextBox1.Text);
                    dt = dt.Date;
                    dt = dt.AddHours(DropDownListHour.SelectedIndex);
                    dt = dt.AddMinutes(DropDownListMinute.SelectedIndex);
                    dictionary[Column.Name] = ConvertEditedValue(dt.ToString());
               }
            }
            catch (System.Exception )
            {
            	
            }

        }

        public override Control DataControl
        {
            get
            {
                return TextBox1;
            }
        }    

    }
}
